﻿using System;
using System.Collections.Generic;
using System.Linq;
using AMM.Business.Project;
using AMM.Business.Store;
using AMM.CustomClassLibrary;
using AMM.CustomWinComponents.Validator;
using AMM.Model;
using AMM.Model.Project;
using AMM.Model.Store;

namespace AMM.UserControls {
    public partial class ProductInventoryEditControl : BaseEditControl {

        private FlexBindingSource<ProductInventory> _fbs;
        private ProductStoreProcessor _materialStoreProcessor;

        public override object BaseEditModel {
            get {
                return this._fbs.CurrentItem;
            }
            set {
                if (this._fbs == null) { this._fbs = new FlexBindingSource<ProductInventory>(); }
                this._fbs.CurrentItem = (ProductInventory)value;
            }
        }

        public ProductInventoryEditControl() {
            this.InitializeComponent();            
        }

        private void BindStoreBasis() {
            StoreBasisProcessor storeBasisProcessor = new StoreBasisProcessor();
            List<StoreBasisViewModel> storeBasisList = storeBasisProcessor.FetchStoreBasis().Where(x => x.IsEnabled).ToList();

            this.ddlStoreBasis.DisplayMemberEx = "Code";
            this.ddlStoreBasis.ValueMemberEx = "Key";
            this.ddlStoreBasis.HeightEx = 300;
            this.ddlStoreBasis.WidthEx = 400;
            this.ddlStoreBasis.PageSizeEx = 20;
            this.ddlStoreBasis.DataBind(new StoreBasisViewModel(), x => new { x.Code, x.Name, x.StoreType }, storeBasisList);
        }

        private void BindProduct() {
            ProductSearchModel searchModel = new ProductSearchModel();
            ProductProcessor productProcessor = new ProductProcessor();
            List<ProductViewModel> productList = productProcessor.FetchProductViewModelList(searchModel);

            this.ddlProduct.DisplayMemberEx = "Code";
            this.ddlProduct.ValueMemberEx = "Key";
            this.ddlProduct.HeightEx = 300;
            this.ddlProduct.WidthEx = 400;
            this.ddlProduct.PageSizeEx = 20;
            this.ddlProduct.DataBind(new ProductViewModel(), x => new { x.ProductType, x.Code, x.Name, x.Remarks }, productList);
        }

        private void CreateFormBindings() {
            this._fbs.CreateBinding(this.ddlProduct, x => x.SelectedValueEx, y => y.ProductId);
            this._fbs.CreateBinding(this.ddlStoreBasis, x => x.SelectedValueEx, y => y.StoreBasisId);
            this._fbs.CreateBinding(this.tbQuantity, x => x.Text, y => y.Quantity, z => z.ToString("G0"));
            this._fbs.CreateBinding(this.tbPreQuantity, x => x.Text, y => y.PreQuantity, z => z.ToString("G0"));
            this._fbs.CreateBinding(this.tbRemarks, x => x.Text, y => y.Remarks);

            this.tbQuantity.KeyUp += (s, e) => {
                this.lblSpanQuantity.Text =
                    (Convert.ToDecimal(this.tbQuantity.Text) - Convert.ToDecimal(this.tbPreQuantity.Text)).ToString("G0");
            };
        }

        private void BindValidation() {

            this.ValidatorForEdit.SetType(this.ddlStoreBasis, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlStoreBasis, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.ddlProduct, ValidationType.Required);
            this.ValidatorForEdit.SetRequiredMessage(this.ddlProduct, this.RequiredMessage);

            this.ValidatorForEdit.SetType(this.tbQuantity, ValidationType.Required | ValidationType.RegularExpression);
            this.ValidatorForEdit.SetRequiredMessage(this.tbQuantity, this.RequiredMessage);
            this.ValidatorForEdit.SetRegularExpression(this.tbQuantity, "^([0-9]+.)?[0-9]+$");
            this.ValidatorForEdit.SetRegularExpressionMessage(this.tbQuantity, "数量只能由数字组成");
        }

        private void ProductInStoreEditControl_Load(object sender, EventArgs e) {
            if (this._fbs == null) { this._fbs = new FlexBindingSource<ProductInventory>(); }
            this._materialStoreProcessor = new ProductStoreProcessor();
            this.BindStoreBasis();
            this.BindProduct();
            this.CreateFormBindings();
            this.BindValidation();
        }

        private void ddlDropDownEx_ValueChanged(object sender, EventArgs e) {
            int materialId = Convert.ToInt32(this.ddlProduct.SelectedValueEx);
            int storeBasisId = Convert.ToInt32(this.ddlStoreBasis.SelectedValueEx);
            ProductStore ms = this._materialStoreProcessor.FetchProductStoreByProductAndStoreBasisId(materialId, storeBasisId);
            if (ms != null) {
                this._fbs.CurrentItem.PreQuantity = ms.Quantity;
            }
        }
    }
}
